package br.com.lis;

import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.OnInitializedListener;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubePlayerFragment;
import com.google.android.youtube.player.YouTubePlayerSupportFragment;
import com.google.android.youtube.player.YouTubePlayerView;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

public class AssistirAula  extends Fragment {
//implements YouTubePlayer.OnInitializedListener {
	//private final static String API_KEY = "AIzaSyCDl7U4RxdTlCWxoH4zCT73M_1orzEIUi";
	//private String ID_VIDEO = "UD92BfzUHgI";
	//private YouTubePlayerView youtube;
	Context ctx;
	Uri src;
	Button  btIrExercicio;
//	YouTubePlayer YPlayer;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.activity_assistir_aula, null);
		ctx = container.getContext();
		
		//youtube = (YouTubePlayerView) view.findViewById(R.id.youtubeView);
		//youtube.initialize(API_KEY, this);

		
		final String curso = getArguments().getString("curso");  
		final String aula = getArguments().getString("aula");  
	
	
		
		// YouTubePlayerFragment youTubePlayerFragment = YouTubePlayerFragment.newInstance();
	//	getFragmentManager().beginTransaction().replace(R.id.youtube_fragment, youTubePlayerFragment).commit();

		/* youTubePlayerFragment.initialize("DEVELOPER_KEY", new OnInitializedListener() {

		        @Override
		        public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
		    		Toast.makeText(ctx, ":)", Toast.LENGTH_SHORT);

		            if (!wasRestored) {
		            	YPlayer  = player;
		                YPlayer.setFullscreen(true);
		                YPlayer.loadVideo("oFcvIlRyHi8");
		                YPlayer.play();
		            }

		        }

		        @Override
		        public void onInitializationFailure(Provider arg0, YouTubeInitializationResult arg1) {
		    		Toast.makeText(ctx, "onInitializationFailure()", Toast.LENGTH_SHORT);

		        }
		    });*/
		
		//final VideoView video = (VideoView) view.findViewById(R.id.vvAssistirAula);
		//final MediaController controller = new MediaController(ctx);
		
		
		
		if(curso.equals("1")){
			if (aula.equals("1")) {
			//	 src = Uri.parse("android.resource://br.com.lis/raw/aula_algoritmos_1");
				VideoFragment f = VideoFragment.newInstance("oFcvIlRyHi8");   
				getFragmentManager().beginTransaction().replace(R.id.youtube_fragment, f).commit();
	
			}
			if (aula.equals("2")) {
				// src = Uri.parse("android.resource://br.com.lis/raw/aula_algoritmos_2");
				VideoFragment f = VideoFragment.newInstance("pe1hsGgS4y4");   
				getFragmentManager().beginTransaction().replace(R.id.youtube_fragment, f).commit();
	
			}
			if (aula.equals("3")) {
				// src = Uri.parse("android.resource://br.com.lis/raw/aula_algoritmos_3");
				VideoFragment f = VideoFragment.newInstance("-jOoE7VdW30");   
				getFragmentManager().beginTransaction().replace(R.id.youtube_fragment, f).commit();
	
			}
			if (aula.equals("4")) {
				// src = Uri.parse("android.resource://br.com.lis/raw/aula_algoritmos_4");
				VideoFragment f = VideoFragment.newInstance("NPw2morLnoA");   
				getFragmentManager().beginTransaction().replace(R.id.youtube_fragment, f).commit();
	
			}
		} else if (curso.equals("2")){
			if (aula.equals("1")) {
				 //src = Uri.parse("android.resource://br.com.lis/raw/aula_matematica_disc_1");
				VideoFragment f = VideoFragment.newInstance("4dzn_CDj9nM");   
				getFragmentManager().beginTransaction().replace(R.id.youtube_fragment, f).commit();
			}
			if (aula.equals("2")) {
				 //src = Uri.parse("android.resource://br.com.lis/raw/aula_matematica_disc_2");
				VideoFragment f = VideoFragment.newInstance("-JQQWlX-I_A");   
				getFragmentManager().beginTransaction().replace(R.id.youtube_fragment, f).commit();
			}	
		}

	  /*      YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();
	        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
	        transaction.add(R.id.youtube_fragment, youTubePlayerFragment).commit();
	        youTubePlayerFragment.initialize("DEVELOPER_KEY", new OnInitializedListener() {

	            @Override
	            public void onInitializationSuccess(Provider arg0, YouTubePlayer youTubePlayer, boolean b) {
	                if (!b) {
	                    YPlayer = youTubePlayer;
	                    YPlayer.setFullscreen(true);
	                    YPlayer.loadVideo("2zNSgSzhBfM");
	                    YPlayer.play();
	                }
	            }

	            @Override
	            public void onInitializationFailure(Provider arg0, YouTubeInitializationResult arg1) {
	                // TODO Auto-generated method stub

	            }
	        });*/
		
		/*video.setVideoURI(src);
		//video.setMediaController(new MediaController(ctx));
		video.setMediaController(controller);
		controller.setMediaPlayer(video);
		
		OnPreparedListener l = new OnPreparedListener() {
			
			@Override
			public void onPrepared(MediaPlayer mp) {
				int duration = video.getDuration();
                video.requestFocus();
                video.start();
                controller.show();				
			}
		};
		video.setOnPreparedListener(l);
		*/
		
		btIrExercicio = (Button) view.findViewById(R.id.btIrExercicio);

		btIrExercicio.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				FragmentTransaction ft;
	    		Fragment frag;
	    		Bundle b = new Bundle();
	    		b.putString("curso", curso);
				b.putString("aula", aula);
				frag = new ExercicioActivity();
				frag.setArguments(b);
				ft = getFragmentManager().beginTransaction();
				ft.replace(R.id.flDrawerHome, frag);
				ft.commit();
			}
		});

		return(view);
	}

	/*@Override
	public void onInitializationFailure(Provider provider, YouTubeInitializationResult error) {
		Toast.makeText(ctx, "onInitializationFailure()", Toast.LENGTH_SHORT);
	}

	@Override
	public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean loadAgain) {
		Log.i("Script", "RAIZ 1");
		if(loadAgain){
			Log.i("Script", "RAIZ 2");
			player.cueVideo(ID_VIDEO);
		}	
	}*/
	
}
